package com.common.utils;

import com.system.entity.Dept;
import com.system.vo.response.DeptRespNodeVO;

import java.util.ArrayList;
import java.util.List;

public class DeptTreeUtil {

    public static List<DeptRespNodeVO> getTree(List<Dept> depts){
        List<DeptRespNodeVO> list=new ArrayList<>();
        for(Dept dept:depts){
            if(dept.getPid().equals("0")) {
                DeptRespNodeVO node = new DeptRespNodeVO();
                node.setId(dept.getId());
                node.setTitle(dept.getName());
                node.setChildren(getChild(dept.getId(),depts));
                list.add(node);
            }
        }
        return list;
    }

    private static List<DeptRespNodeVO> getChild(String pid,List<Dept> depts){
        List<DeptRespNodeVO> list=new ArrayList<>();
        for(Dept dept:depts)
            if(dept.getPid().equals(pid)){
                DeptRespNodeVO node = new DeptRespNodeVO();
                node.setId(dept.getId());
                node.setTitle(dept.getName());
                node.setChildren(getChild(dept.getId(),depts));
                list.add(node);
            }
            return list;
    }
}
